#include "stdio.h"
#include "malloc.h"
#define datatype int

typedef struct dnode
{
	datatype data;
	struct dnode *prior,*next;
}Dlinklist;
Dlinklist *head;

Dlinklist *CreateDListF();
void Dinsert(Dlinklist *p,datatype x);
void Ddelete(Dlinklist *p);
void ShowSList(Dlinklist* head);

int main()
{
	Dlinklist *p;
	head=CreateDListF();
	printf("The original list:\n");
	ShowSList(head);

	p=head;
	p=p->next;
    Dinsert(p,99);
	printf("Insert before the 2nd node:\n");
	ShowSList(head);

	Ddelete(p);
	printf("Delete the 3rd node:\n");
	ShowSList(head);
}

Dlinklist *CreateDListF()    /*头插法*/
{
	datatype ch;
	Dlinklist *head,*s;
	head=NULL;          /*链表开始为空*/
	printf("Please intput integer until '0':\n");
	scanf("%d",&ch);    /*读入第一个结点的值*/
	while(ch!=0)
	{
		s=(Dlinklist *)malloc(sizeof(Dlinklist));  /*生成新结点*/
		s->data=ch;
		s->next=head;
		if (head!=NULL)head->prior=s;
		head=s;
		printf("Please intput integer until '0':\n");
		scanf("%d",&ch);
    }
    return head;
}        /*CreateDListF*/


void Dinsert(Dlinklist *p,datatype x)    /*(前)插入操作*/
{
	Dlinklist *s;
	s=(Dlinklist *)malloc(sizeof(Dlinklist));
	s->data=x;
	s->prior=p->prior;
	s->next=p;
	p->prior->next=s;
	p->prior=s;
}/*Dinsert*/

void Ddelete(Dlinklist *p)
{
	p->prior->next=p->next;
	p->next->prior=p->prior;
	free(p);
}/*Ddelete*/

void ShowSList(Dlinklist* head)
{
	Dlinklist *p;
	p=head;

	if (p->data==0) p=p->next;  /*对有头结点的链表进行特殊处理*/

	while(p->next!=0)
	{
		printf("%d<==>",p->data);
		p=p->next;
	}
	printf("%d\n\n",p->data);
}

